const app = getApp()
import Dialog from '@vant/weapp/dialog/dialog';
import {
    showMsg
} from '../../../../utils/config.js'
import {
    reqAlarmReceiverList,
    reqalarmReveiverDel,
    reqReceiverTimeupdate,
    reqReceiverTimeget,
    reqGetNormalUser
} from '../../../../request/api'
Page({
    data: {
        navHeight: app.globalData.navHeight + 'px', //头部距离
        bottomHeight: app.globalData.bottomHeight + 'px', //iphone底部距离
        dataList: [{
            name: "张天龙",
            mobile: "18118715218",
            sfz: "430682200006161037",
        },{
            name: "刘乐乐",
            mobile: "19952526565",
            sfz: "",
        },{
            name: "陈极冰",
            mobile: "13284949498",
            sfz: "430682200006161037",
        },{
            name: "力天使",
            mobile: "15595498888",
            sfz: "",
        },],
        page: 1,
        searchValue: '', //输入的搜索值
        active: false, //姓名/手机号是否展示
        searchType: 'username',
        timer: '', //搜索值定时
        isNextPage: true, //是否下一页
        triggered: false, //是否下拉刷新列表
        showMoreActions: false, //是否展示更多操作
        listItemInfo: {}, //点击更多时列表一项信息
    },

    onLoad() {
    },
    onShow() {
        this.getGetNormalUser()
    },
    async getGetNormalUser(){
        const res = await reqGetNormalUser({
            nickname:"",
            mobile:""
        })
        if (res.code !== 20000) return showMsg(res.message)
        console.log(res);
    },
    /**
     * @name: 获取接警人列表
     * @msg: *parkId：园区id  *pid：父id  name：空间名称
     * @return {*}
     */
    async getDataList() {
        let {
            searchType,
            searchValue
        } = this.data
        let name = ''
        let mobile = ''
        if (searchType === 'username') {
            name = searchValue
            mobile = ''
        } else {
            name = ''
            mobile = searchValue
        }
        let params = {
            orgId: app.globalData.orgId ==1?'':app.globalData.orgId,
            name,
            mobile,
            userId:app.globalData.userId,
            roleName:app.globalData.rolename,
        }
        const res = await reqAlarmReceiverList(this.data.page, 10, params)
        if (res.code !== 20000) return showMsg(res.message, "error")
        let {
            rows
        } = res.data
        if (this.data.page === 1) {
            this.setData({
                dataList: rows,
                isNextPage: true
            })
        } else if (this.data.page > 1 && rows.length) {
            this.setData({
                dataList: this.data.dataList.concat(rows),
                isNextPage: true
            })
        } else if (this.data.page > 1 && !rows.length) {
            showMsg('已经到底啦~')
            this.setData({
                isNextPage: false
            })
        }
    },
    // 滚动到底部触发
    lower() {
        if (!this.data.isNextPage) return
        this.setData({
            page: this.data.page + 1,
        })
        this.getDataList()
    },
    // 下拉刷新
    async onRefresh() {
        this.setData({
            dataList: [],
            page: 1,
            isNextPage: true,
            triggered: true,
        })
        await this.getDataList()
        this.setData({
            triggered: false,
        })
    },
    // 输入框搜索
    // searchChange(e) {
    //     this.setData({
    //         searchValue: e.detail,
    //         page: 1,
    //         isNextPage: true
    //     })
    //     clearTimeout(this.data.timer)
    //     this.data.timer = setTimeout(() => {
    //         this.getDataList()
    //     }, 500)
    // },
    // 点击切换用户名/手机号
    toggleSearchClick() {
        this.setData({
            active: !this.data.active
        })
    },
    // 切换搜索条件
    itemClick(e) {
        let {
            type
        } = e.currentTarget.dataset
        this.setData({
            searchType: type,
            active: !this.data.active
        })
    },
    // 获取输入值
    searchValueChange(e) {
        this.setData({
            searchValue: e.detail.value
        })
    },
    // 清除搜索输入值
    clearUsername() {
        this.setData({
            searchValue: ''
        })
        this.getInitDataList()
    },
    // 点击搜索
    async searchClick() {
        let {
            searchValue
        } = this.data
        if (!searchValue) return showMsg("请先输入搜索信息")
        // if (searchType === 'username') {
        //     // params.username = searchValue

        // } else {
        //     // params.mobile = searchValue
        // }
        this.getInitDataList()
    },
    // 删除
    delClick() {
        this.setData({
            user: {}
        })
    },
    // 操作列表后回到最初状态
    getInitDataList() {
        this.setData({
            page: 1,
            isNextPage: true,
            dataList: []
        })
        this.getDataList()
    },

    // 去新增接警人
    goAddAlarmReceiver() {
        let info = {
            // parkId,
            spaceId: '',
            spaceName: ''
        }
        let params = JSON.stringify(info)
        wx.navigateTo({
            url: `/packageA/pages/index-inside-page/owner-add/owner-add?params=${params}`,
        })
    },
    // 去接警人详情
    goDetail(e) {
        let {
            item
        } = e.currentTarget.dataset
        let info = JSON.stringify(item)
        wx.navigateTo({
            url: `/packageA/pages/index-inside-page/owner-detail/owner-detail?item=${info}`,
        })
    },
      // 点击删除
      clickDelete() {
        let {
            listItemInfo
        } = this.data
        Dialog.confirm({
                title: '温馨提示',
                message: '此操作将删除该接警人，是否继续？',
            })
            .then(async () => {
                const res = await reqalarmReveiverDel(listItemInfo.id)
                if (res.code != 20000) return showMsg(res.message);
                this.getInitDataList()
                showMsg("删除成功")
                this.unShowMoreActions()
            })
            .catch(() => {});
    },
      // 点击更多操作
      clickMore(e) {
        let {
            item
        } = e.currentTarget.dataset
        this.setData({
            listItemInfo: item,
            showMoreActions: true
        })
    },
})